package org.rqb;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.mouse.NativeMouseEvent;
import com.github.kwhat.jnativehook.mouse.NativeMouseListener;
import com.github.kwhat.jnativehook.mouse.NativeMouseMotionListener;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import lombok.extern.slf4j.Slf4j;
import org.rqb.config.MyConfig;
import org.rqb.frame.GestureWindow;
import org.rqb.frame.MyFrame;
import org.rqb.listener.GlobalMouseListener;
import org.rqb.panel.GesturePanel;
import org.rqb.socket.MySocket;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import static org.rqb.Constant.EXIST;
import static org.rqb.Constant.PORT;

@Slf4j
public class Main {

    private static MyFrame myFrame;
    private static final MySocket mySocket = MySocket.getInstance();
    private static final Properties config = MyConfig.getConfig();
    public static GestureWindow gestureWindow;

    public static void main(String[] args) {

        // 先检查端口是否已经被占用（即程序是否已经在运行）
        if (mySocket.isPortInUse(PORT)) {
            // 如果端口被占用，说明程序已经运行，发送请求展示JFrame
            mySocket.sendShowWindowRequest(PORT);

            System.exit(0);  // 阻止新实例启动
        }
        // 启动界面
        SwingUtilities.invokeLater(() -> {
            myFrame = new MyFrame();
            if ("true".equals(config.getProperty(Constant.AUTO_HIDE_WINDOW_ONSTART))){
                myFrame.setVisible(false);
            } else {
                myFrame.setVisible(true);
            }
            // 启动Socket监听
            mySocket.startSocketServer(PORT,myFrame);


            //开启快捷手势
            if ("true".equals(config.get(Constant.GESTURE_ENABLED))){
                //开启手势界面windows
                gestureWindow = GestureWindow.getInstance();
                gestureWindow.startMouseGestures();

            }
        });





    }



}
